package com.programmercarl.array;

/**
 * https://leetcode.cn/problems/sqrtx/description/
 *
 * @author yulongtian
 * @create 2024-12-10 10:57
 */
public class Test04 {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(2147395599);
        System.out.println(mySqrt(2147395599));
    }

    public static int mySqrt(int x) {
        long left = 0L;
        long right = x;
        long res = 0L;
        while (left <= right) {
            long mid = left + ((right - left) / 2);

            if (mid * mid > x) {
                right = mid - 1;
            } else if (mid * mid < x) {
                res = mid;
                left = mid + 1;
            } else {
                return (int) mid;
            }
        }

        return (int) res;
    }
}
